package cn.dulian.blob;

import cn.dulian.util.JDBCUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Description 测试使用PreparedStatement操作Blob类型的数据
 * @author dulian
 * @create 2022-04-05 11:04
 */
public class BlobTest {
    //向数据表customers中插入Blob类型的字段
    @Test
    public void testInsert() throws SQLException, IOException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,"袁浩");
        ps.setObject(2, "yuan@qq.com");
        ps.setObject(3,"1992-09-08");
        FileInputStream is = new FileInputStream(new File("photon.jpg"));
        ps.setBlob(4, is);
        ps.execute();

        JDBCUtils.closeResource(conn, ps);

    }
}
